<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin_shicai extends Admin_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('Shicai_mdl');
	}

	public function index()
	{
		$this->shicai_list();
	}

	public function shicai_list()
	{
		$keywords = $this->input->post('keywords');
		$page = max(1, intval($this->input->post('pageNum')));
		$limit = 18; //每页显示数
		$offset = ($page - 1) * $limit;


		//分页
		$data['totalCount'] = 10000;
		$data['numPerPage'] = $limit;
		$data['currentPage'] = $page;
		$data['keywords'] = $keywords;
		if ($keywords)
		{
			$data['shicai_list'] = $this->Shicai_mdl->get_shicai_search($keywords, $offset, $limit)->result_array();
		}
		else
		{
			$data['shicai_list'] = $this->Shicai_mdl->get_shicai_list('', $offset, $limit)->result_array();
		}
		
		$this->load->view('shicai', $data);
	}

	public function shicai_add()
	{
		if ($this->input->post('submit') == 'ok')
		{
			//格式化trophos数组
			$trophos = $this->input->post('trophos');
			$tropho = '';
			for($i=0;$i<count($trophos['str']);$i++) 
			{ 
				($i > 0) && $tropho.=',';
				$tropho.=$trophos['str'][$i].'-'.$trophos['int'][$i];

			}

			//格式化xiangke数组
			$xiangke = $this->input->post('xiangke');
			$xiangkes = '';
			for($i=0;$i<count($xiangke['shicai']);$i++) 
			{ 
				($i > 0) && $xiangkes.=',';
				$xiangkes.=$xiangke['shicai'][$i].'-'.$xiangke['yingxiang'][$i];

			}

			//格式化xiangsheng数组
			$xiangsheng = $this->input->post('xiangsheng');
			$xiangshengs = '';
			for($i=0;$i<count($xiangsheng['shicai']);$i++) 
			{ 
				($i > 0) && $xiangshengs.=',';
				$xiangshengs.=$xiangsheng['shicai'][$i].'-'.$xiangsheng['yingxiang'][$i];

			}

			$arr = array(
				'title' => $this->input->post('title'),
				'alias' => $this->input->post('alias'),
				'xiangsheng' => $xiangshengs,
				'xiangke' => $xiangkes,
				'intro' => $this->input->post('intro'),
				'tropho' => $tropho,
				'effect' => $this->input->post('effect'),
				'worth' => $this->input->post('worth'),
				'crowd' => $this->input->post('crowd'),
				'pick' => $this->input->post('pick'),
				'store' => $this->input->post('store'),
				'views' => $this->input->post('views'),
				'pid' => $this->input->post('pid'),
				'classid' => $this->input->post('classid'),
				'pic' => $this->input->post('pic_fileName'),
				'time' => strtotime($this->input->post('time')),
				'season' => $this->input->post('season')
			);
			$result = $this->Shicai_mdl->shicai_insert($arr);
			$this->_jui_message('操作成功');
		}
		else
		{
			$data['shicai_info'] = 0;
			$this->load->view('shicai_edit', $data);
		}
	}
	public function shicai_edit($id = '')
	{
		if(empty($id) || !is_numeric($id))
		{
			show_error('失败：参数有误');
			exit();
		}

		if ($this->input->post('submit') == 'ok')
		{
			//格式化trophos数组
			$trophos = $this->input->post('trophos');
			$tropho = '';
			for($i=0;$i<count($trophos['str']);$i++) 
			{ 
				($i > 0) && $tropho.=',';
				$tropho.=$trophos['str'][$i].'-'.$trophos['int'][$i];

			}

			//格式化xiangke数组
			$xiangke = $this->input->post('xiangke');
			$xiangkes = '';
			for($i=0;$i<count($xiangke['shicai']);$i++) 
			{ 
				($i > 0) && $xiangkes.=',';
				$xiangkes.=$xiangke['shicai'][$i].'-'.$xiangke['yingxiang'][$i];

			}

			//格式化xiangsheng数组
			$xiangsheng = $this->input->post('xiangsheng');
			$xiangshengs = '';
			for($i=0;$i<count($xiangsheng['shicai']);$i++) 
			{ 
				($i > 0) && $xiangshengs.=',';
				$xiangshengs.=$xiangsheng['shicai'][$i].'-'.$xiangsheng['yingxiang'][$i];

			}

			$arr = array(
				'title' => $this->input->post('title'),
				'alias' => $this->input->post('alias'),
				'xiangsheng' => $xiangshengs,
				'xiangke' => $xiangkes,
				'intro' => $this->input->post('intro'),
				'tropho' => $tropho,
				'effect' => $this->input->post('effect'),
				'worth' => $this->input->post('worth'),
				'crowd' => $this->input->post('crowd'),
				'pick' => $this->input->post('pick'),
				'store' => $this->input->post('store'),
				'views' => $this->input->post('views'),
				'pid' => $this->input->post('pid'),
				'classid' => $this->input->post('classid'),
				'pic' => $this->input->post('pic_fileName'),
				'time' => strtotime($this->input->post('time')),
				'season' => $this->input->post('season')
			);

			$result = $this->Shicai_mdl->shicai_update($arr, $id);
			$this->_jui_message('操作成功');
		}
		else
		{
			$data['shicai_info'] = $this->Shicai_mdl->get_shicai_by_id($id);
			/** 不存在? */
			if(!$data['shicai_info'])
			{
				redirect('shicai/category');
				exit();
			}

			$this->load->view('shicai_edit', $data);
		}

	}
	public function shicai_del($id = '')
	{
		$result = $this->Shicai_mdl->shicai_delete($id);
		$this->_jui_message('操作成功');
	}
}

/* End of file shicai.php */
/* Location: ./application/controllers/manage/shicai.php */